package p2pscrabble;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

// this will create dictionary by separating the words
// according first letter and length of words
// to find a word, the system opens dictionary file which contain the word
public class createDictionary {

	public static void main(String[] args) {

		String file_dir = "Y:\\P2PScrabble\\P2PScrabble\\src\\dict\\";

		File file = new File(file_dir + "csw.txt");
		FileReader in_file = null;
		BufferedReader in = null;

		FileWriter out_file = null;
		BufferedWriter out = null;

		try {
			in_file = new FileReader(file);

			in = new BufferedReader(in_file);
			String line = in.readLine();

			while (!line.equals("") || line != null) {
				String word = (line.split(" "))[0];

				char first = word.charAt(0);
				int count = word.length();

				out_file = new FileWriter(file_dir + first + count + ".txt",
						true);
				out = new BufferedWriter(out_file);
				out.write(word + "\n");
				out.close();

				System.out.println(word);
				line = in.readLine();
			}

			// dispose all the resources after using them.
			in.close();
			in_file.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
